package bbs.service.impl;

import bbs.entity.dto.MapDto;
import bbs.entity.vo.ResponseVO;
import bbs.mappers.ForumArticleMapper;
import bbs.mappers.UserInfoMapper;
import bbs.service.MapService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mapService")
public class MapServiceImpl implements MapService {

    private static final Logger logger = LoggerFactory.getLogger(MapServiceImpl.class);


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ForumArticleMapper forumArticleMapper;

    @Override
    public  List<MapDto> getAreaLevel() {
        List<MapDto> usersMap = userInfoMapper.userAreaCount();
        List<MapDto> articlesMap = forumArticleMapper.articleAreaCount();

        List<MapDto> result = combineAndCalculate(usersMap, articlesMap);
        logger.info("result:{}", result);

        return combineAndCalculate(usersMap, articlesMap);
    }

    private List<MapDto> combineAndCalculate(List<MapDto> usersMap, List<MapDto> articlesMap) {

        List<MapDto> resultList = new ArrayList<>();

        for (MapDto userInfo : usersMap) {
            MapDto result = new MapDto();
            for (MapDto article : articlesMap) {
                if (userInfo.getArea().equals(article.getArea())) {
                    Integer count = userInfo.getCount() * 5 + article.getCount();
                    result.setCount(count);
                }
                result.setArea(userInfo.getArea());
            }
            if (result.getCount() == null) {
                result.setCount(userInfo.getCount() * 5);
            }
            resultList.add(result);
        }

        for (MapDto article : articlesMap) {
            Boolean flag = true;
            for (MapDto userInfo : resultList) {
                if (userInfo.getArea().equals(article.getArea())) {
                    flag = false;
                    break;
                }
            }
            if (flag){
                MapDto result = new MapDto();
                result.setCount(article.getCount());
                result.setArea(article.getArea());
                resultList.add(result);
            }
        }

        return resultList;
    }
}
